/*
 * $Id: DirectoryStore.java,v 1.6 2009/07/21 19:27:00 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.util;

import java.io.File;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.util.ASCFileHandler;
import com.ge.healthcare.autosc.onwatch.pojo.DirNode;
/**
 * It is a tree structure to hold registering directories. The class allows
 * user to create directories and manage them through the provided operations. 
 * @author 212042946
 *
 */
public class DirectoryStore {

	public static final String BATCH_DIR = "batch";
	public static final String REQUESTLIST_DIR = "requestList";
	public static final String REQUEST_DIR = "request";
	private DirNode root = null;

	/**
	 * Constructor
	 * @param rootDirStr
	 */
	public DirectoryStore(String rootDirStr) {
		File rootDir = new File(rootDirStr);
		root = new DirNode(rootDir);
	}
	/**
	 * Constructor
	 * @param rootDir
	 */
	public DirectoryStore(File rootDir) {
		root = new DirNode(rootDir);
	}
	
	/**
	 * Get the root directory in the directory store. This is
	 * relative to the directory specified in constructor
	 * @return Root directory in file object
	 */
	public File getRootFile() {
		File retFile = null;
		if(root != null) {
			retFile = root.getFile();
		}
		return retFile;
	}
	
	/**
	 * WARNING: This method will abandon all registered directories inside
	 * the store and re-establish from scratch. If you want to manipulate
	 * previously registered directory after this method call, you have to 
	 * do it manually yourself.  
	 * @param rootId
	 */
	public void recreateStore(String rootDirStr) {
		File rootDir = new File(rootDirStr);
		root = new DirNode(rootDir);
	}
	
	/**
	 * Added node to the root.
	 * @param dirName
	 * @return
	 */
	public File addDirEntry(String dirName) {
		File retFile = null;
		if(root != null) {
			DirNode childNode = root.addChild(dirName);
			if(childNode == null) {
				ASCLogger.debug(this.getClass(), "addDirEntry", "child Node is null for dirName " + dirName);
			} else {
				retFile = childNode.getFile();
			}
		}
		return retFile;
	}
	
	/**
	 * Add directory node right under directory path.
	 * @param searchPath pipe-separated string representing the directory path. This
	 *                   is used for searching directory under which the new directory
	 *                   is created.
	 * @param dirName	Name of the directory to be created
	 * @return	New directory file object
	 */
	public File addDirEntry(String searchPath, String dirName) {
		File retFile = null;
		if(root != null) {
			DirNode parentNode = root.findChild(searchPath);
			if(parentNode != null) {
				DirNode childNode = parentNode.addChild(dirName);
				retFile = childNode.getFile();
			}
		}
		return retFile;
	}
	/**
	 * Get directory file object
	 * @param searchPath	Path to find the directory
	 * @return
	 */
	public File getDirEntry(String searchPath) {
		File retFile = null;
		if(root != null) {
			DirNode childNode = root.findChild(searchPath);
			if(childNode != null) {
				retFile = childNode.getFile();
			}
		}
		return retFile;
	}
	/**
	 * Delete root directory and all its content
	 */
	public void deleteRootDir() {
		if(root != null) {
			ASCFileHandler.deleteTree(root.getFile());
			root = null;
		}
	}
	/**
	 * Determine if directory store is empty
	 * @return
	 */
	public boolean isStoreEmpty() {
		return ((root == null)?true:false);
	}
	
	/**
	 * Append path to the existing search path. This is used
	 * for building directory search path.
	 * @return
	 */
	public static String addPath(String path, String dirname) {
		return path + DirNode.PATH_SEP + dirname;
	}
}
